import 'package:flutter/material.dart';

import '../constant/colors.dart';

enum MagicButtonIconPosition { top, left, bottom, right }

/// 按钮，可以设置圆角和渐变背景。
class MagicButton extends StatelessWidget {
  /// 固定宽度
  final double? width;
  final double? minWidth;

  /// 固定高度
  final double? height;

  /// 圆角值
  final double radius;

  /// 背景色。如果 [gradient] 和 [color] 都有值，优先使用 [gradient]。
  final Color? color;

  /// 渐变背景。如果 [gradient] 和 [color] 都有值，优先使用 [gradient]。
  final Gradient? gradient;

  /// 边框宽
  final double? borderWidth;

  /// 边框颜色
  final Color? borderColor;

  final bool isDisabled;

  /// 点击回调
  final VoidCallback? onTap;

  /// 按钮图标或标题
  final Widget child;

  /// 外间距
  final EdgeInsetsGeometry? margin;

  /// 内间距
  final EdgeInsetsGeometry? padding;

  final AlignmentGeometry? alignment;

  final Decoration? decoration;

  const MagicButton({
    super.key,
    this.width,
    this.minWidth,
    this.height,
    this.radius = 0.0,
    this.color,
    this.gradient,
    this.borderWidth,
    this.borderColor,
    this.isDisabled = false,
    this.onTap,
    this.margin,
    this.padding,
    this.alignment,
    this.decoration,
    required this.child,
  });

  /// 带图标和标题带按钮。
  factory MagicButton.iconAndTitle({
    Key? key,
    double? width,
    double? minWidth,
    double? height,
    double? radius,
    Color? color,
    LinearGradient? gradient,
    bool? isDisabled,
    VoidCallback? onTap,
    AlignmentGeometry? alignment,
    EdgeInsetsGeometry? margin,
    EdgeInsetsGeometry? padding,
    required Widget icon,
    required Widget title,
    MagicButtonIconPosition? iconPosition,
    double? spacing,
  }) = _MagicButtonWithIcon;

  /// 主题按钮，蓝色渐变背景，6圆角
  const MagicButton.theme({
    super.key,
    this.width,
    this.minWidth,
    this.height,
    this.isDisabled = false,
    this.onTap,
    this.margin,
    this.padding,
    this.radius = 6,
    this.alignment,
    this.decoration,
    required this.child,
  })  : color = null,
        gradient = MagicColors.themeGradient,
        borderWidth = null,
        borderColor = null;

  @override
  Widget build(BuildContext context) {
    if (isDisabled) {
      return Opacity(opacity: 0.5, child: content);
    }
    return onTap == null ? content : InkWell(onTap: onTap, child: content);
  }

  Widget get content => Container(
        decoration: decoration ??
            BoxDecoration(
              color: color,
              gradient: gradient,
              borderRadius: radius > 0.0 ? BorderRadius.circular(radius) : null,
              border: border,
            ),
        width: width,
        height: height,
        constraints: minWidth != null ? BoxConstraints(minWidth: minWidth ?? 0) : null,
        margin: margin,
        padding: padding,
        alignment: alignment,
        child: child,
      );

  BoxBorder? get border {
    final width = borderWidth;
    final color = borderColor;
    if (width == null || color == null || width <= 0.0) {
      return null;
    }
    return Border.all(color: color, width: width);
  }
}

class _MagicButtonWithIcon extends MagicButton {
  _MagicButtonWithIcon({
    super.key,
    super.color,
    super.width,
    super.minWidth,
    super.height,
    double? radius,
    LinearGradient? super.gradient,
    bool? isDisabled,
    super.onTap,
    super.alignment,
    super.margin,
    super.padding,
    required Widget icon,
    required Widget title,
    MagicButtonIconPosition? iconPosition,
    double? spacing,
  }) : super(
          radius: radius ?? 0,
          isDisabled: isDisabled ?? false,
          child: _IconAndTitle(
            icon: icon,
            title: title,
            iconPosition: iconPosition ?? MagicButtonIconPosition.left,
            spacing: spacing ?? 0,
          ),
        );
}

class _IconAndTitle extends StatelessWidget {
  final Widget icon;
  final Widget title;
  final MagicButtonIconPosition iconPosition;
  final double spacing;

  const _IconAndTitle({
    this.iconPosition = MagicButtonIconPosition.left,
    this.spacing = 0,
    required this.icon,
    required this.title,
  });

  @override
  Widget build(BuildContext context) {
    switch (iconPosition) {
      case MagicButtonIconPosition.top:
        return Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            icon,
            if (spacing > 0) SizedBox(width: spacing),
            title,
          ],
        );
      case MagicButtonIconPosition.left:
        return Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            icon,
            if (spacing > 0) SizedBox(width: spacing),
            title,
          ],
        );
      case MagicButtonIconPosition.right:
        return Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            title,
            if (spacing > 0) SizedBox(width: spacing),
            icon,
          ],
        );
      default:
        return Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            icon,
            if (spacing > 0) SizedBox(width: spacing),
            title,
          ],
        );
    }
  }
}
